package com.example.moblie203zlw20216061300508;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import fragment.BlankFragment;
import fragment.newFragment;
import fragment.testFragment;

public class MainActivity extends AppCompatActivity {
    private List<Fragment> list = new ArrayList<>();
    private TabLayout tabLayout;
    private ViewPager2 vp2;
    private TabItem neww;
    private TabItem testt;
    private TabItem wy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        vp2 = (ViewPager2) findViewById(R.id.vp_2);
        list.add(BlankFragment.newInstance("告知",""));
        list.add(newFragment.newInstance("新闻",""));
        list.add(testFragment.newInstance("通知",""));
        vp_Adapter vp_adapter = new vp_Adapter(getSupportFragmentManager(), getLifecycle(), list);
        vp2.setAdapter(vp_adapter);
        new TabLayoutMediator(tabLayout, vp2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //getArguments()是Activity与Fragment之间的传值
                tab.setText(list.get(position).getArguments().getString("param1"));
            }
        }).attach();
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        vp2 = (ViewPager2) findViewById(R.id.vp_2);
    }

}